package com.sti.platform.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aisino.core.common.JsonPost;
import com.aisino.core.controller.BaseController;
import com.sti.platform.model.Company;
import com.sti.platform.service.CompanyService;

@Controller
@RequestMapping("company")
public class CompanyController extends BaseController {
	@Autowired
	private CompanyService companyService;
	
	@RequestMapping("list")
	public String queryList(HttpServletRequest request,String params,Integer index,Integer row){
		List<Company> list = companyService.queryList(params, index, row);
		request.setAttribute("list", list);
		return "company/index";
	}
	
	@RequestMapping("addHtml")
	public String addHtml(HttpServletRequest request){
		List<Company> list = companyService.queryCompanys();
		request.setAttribute("list", list);
		return "company/add";
	}
	
	@RequestMapping("add")
	public @ResponseBody Map addCompany(final Company company){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){
				companyService.addCompany(company);
			}
		});
	}
	
	@RequestMapping("editorHtml")
	public String editorHtml(HttpServletRequest request){
		String companyId = request.getParameter("companyId");
		Company company = companyService.queryCompany(companyId);
		List<Company> list = companyService.queryCompanys();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("company", company);
		request.setAttribute("map", map);
		return "company/editor";
	}
	
	@RequestMapping("editor")
	public @ResponseBody Map editorCompany(final Company company){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){
				companyService.updateCompany(company);
			}
		});
	}
	
	@RequestMapping("delect")
	public @ResponseBody Map delectCompany(final String companyId){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){
				companyService.delectCompany(companyId);
			}
		});
	}
	
	@RequestMapping("delects")
	public @ResponseBody Map delectCompanys(final String companyIds){
		return super.jsonPost(new JsonPost(){
			public void doInstancePost(Map map){
				companyService.delectCompanys(companyIds);
			}
		});
	}

}
